<template>
  <el-card>
    <el-table :data="skuList" border v-loading="loading">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="描述" prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片 ">
        <template v-slot="{ row }">
          <el-image
            :src="row.skuDefaultImg"
            style="width: 100px; height: 100px"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="重量(KG)" prop="weight"></el-table-column>
      <el-table-column label="价格(元)" prop="price"></el-table-column>
      <el-table-column label="操作" width="300px">
        <template v-slot="{ row, $index }">
          <el-tooltip content="上架" placement="top" v-if="row.isSale === 0">
            <el-button
              type="success"
              :icon="Top"
              size="small"
              @click="skuOnSale(row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="下架" placement="top" v-else>
            <el-button
              type="danger"
              :icon="Bottom"
              size="small"
              @click="skuCancleSale(row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="修改Sku" placement="top">
            <el-button type="primary" :icon="Edit" size="small"></el-button>
          </el-tooltip>
          <el-tooltip content="查看详情" placement="top">
            <el-button
              type="primary"
              :icon="InfoFilled"
              size="small"
              @click="getSkuInfo(row.id)"
            ></el-button>
          </el-tooltip>
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="真的要删除此条sku数据么?"
            @confirm="deleteSkuInfo(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                title="删除Sku"
                size="small"
                :icon="Delete"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout=" prev, pager, next, jumper,->, sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 抽屉效果实现 -->
    <el-drawer
      v-model="drawer"
      title="查看sku详情"
      :direction="direction"
      size="40%"
    >
      <el-row :gutter="10">
        <el-col :span="5" align="right">
          <div>名称:</div>
        </el-col>
        <el-col :span="16">
          <div>{{ skuInfo.skuName }}</div>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="5" align="right">
          <div>描述:</div>
        </el-col>
        <el-col :span="16">
          <div>{{ skuInfo.skuDesc }}</div>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="5" align="right">
          <div>价格:</div>
        </el-col>
        <el-col :span="16">
          <div>{{ skuInfo.price }}</div>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="5" align="right">
          <div>平台属性:</div>
        </el-col>
        <el-col :span="16">
          <el-tag
            v-for="attrValue in skuInfo.skuAttrValueList"
            :key="attrValue.id"
            >{{ attrValue.valueName }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="5" align="right">
          <div>销售属性:</div>
        </el-col>
        <el-col :span="16">
          <el-tag
            v-for="saleAttrValue in skuInfo.skuSaleAttrValueList"
            :key="saleAttrValue.id"
            >{{ saleAttrValue.saleAttrValueName }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="5" align="right">
          <div>商品图片:</div>
        </el-col>
        <el-col :span="16">
          <el-carousel height="250px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <el-image
                :src="item.imgUrl"
                style="width: 100%; height: 100%"
              ></el-image>
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </el-card>
</template>

<script lang="ts">
export default {
  name: 'Sku',
}
</script>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { InfoFilled, Edit, Delete, Top, Bottom } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import {
  reqGetAllSkuListData,
  reqSkuOnSale,
  reqSkuCancleSale,
  reqDeleteSkuInfo,
  reqGetOneSkuInfo,
} from '@/api/product/sku'

const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(5)
const total = ref(1)
const skuList = ref([])

// 定义一个skuInfo对象 用来渲染抽屉中的效果
const skuInfo = ref([])

// 抽屉相关的变量
const drawer = ref(false)
const direction = ref('rtl')

// 分页器相关的变量
const small = ref<boolean>(false)
const background = ref<boolean>(false)
const disabled = ref<boolean>(false)

// 1.定义一个获取skuList列表的函数，如果需要获取skuList数据的话，可以直接调用该函数
const getAllSkuListData = async () => {
  loading.value = true
  const result = await reqGetAllSkuListData(currentPage.value, pageSize.value)
  // console.log('123', result)

  skuList.value = result.records
  total.value = result.total
  loading.value = false
}

// 2.组件一加载完毕就发送请求获取数据
onMounted(() => {
  getAllSkuListData()
})
// 分页相关的两个函数
// 3.改变每页显示的数量
const handleSizeChange = (val: number) => {
  // console.log(`${val} items per page`)
  pageSize.value = val //这个好像是更改每页显示的数量
  currentPage.value = 1 //跳转到第一页
  getAllSkuListData()
}
// 4.点击页码的时候执行的函数
const handleCurrentChange = (val: number) => {
  // console.log(`current page: ${val}`)
  currentPage.value = val
  getAllSkuListData()
}

// 5.sku上架
const skuOnSale = async (row) => {
  await reqSkuOnSale(row.id)
  row.isSale = 1 - row.isSale
}
// 5.sku下架
const skuCancleSale = async (row) => {
  await reqSkuCancleSale(row.id)
  row.isSale = 1 - row.isSale
}

// 7. 删除sku
const deleteSkuInfo = async (id: number) => {
  await reqDeleteSkuInfo(id)

  ElMessage.success('删除sku成功...')
  // 重新渲染剩余的数据
  getAllSkuListData()
}

// 8. 查看skuInfo详情信息
// 这个是获取一条sku的信息，用来在sku管理的查看详情那里的抽屉中展示
const getSkuInfo = async (id: number) => {
  const result = await reqGetOneSkuInfo(id)
  console.log('drawer', result)
  drawer.value = true

  skuInfo.value = result
}
</script>
<style></style>
